import app from '../App.vue';
import {getCookie} from "@/utils/cookie.js";
import {getUrl,logout} from '@/utils/common.js';

// const baseURL = 'http://192.168.4.111:8085/fj/api/testMall/';
const baseURL = 'https://game-rfs.3yakj.com/fj/api/testMall/';
// const baseURL = 'https://game-rfs.3yakj.com/fj/api/mall/';
const http = (options) => {
    return new Promise((resolve, reject) => {
        // uni.showLoading({
        //     title: '加载中...',
        //     mask: options.load || false // 默认遮罩出现可以继续操作
        // });
        try{
            uni.request({
                url: (options.baseURL || baseURL) + options.url,
                method: options.method || 'POST', // 默认为POST请求
                data: options.data, //请求超时在manifest.json配置
				timeout: 10000,
                header: {
                    'token': getUrl('token'),
                    'Content-Type': options.header == 'form' ? 'application/x-www-form-urlencoded' : 'application/json',
					'time': Date.parse(new Date())
                },
                success: res => {
					//resolve(res.data)
					if(res.data.status===200){
						resolve(res.data);
					}
					else if(res.data.status===401){
						reject(res.data);
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: '登录状态失效，请重新登录！',
							success: function(res) {
								if (res.confirm) {
									logout();
								}
							}
						});
						return;
					}
					else{
						reject(res.data);
						// uni.hideLoading();
						setTimeout(function() {
							uni.showToast({
								title: res.data.msg?res.data.msg:'服务器无返回',
								icon:'none',
								duration: 2000
							})
							setTimeout(function() {
								uni.hideToast();
							}, 2000);
						}, 100);
					}
                },
                fail: (err) => {
                    reject(err.data);
                    console.log(err.data);
                    uni.showToast({
                        title: '服务器异常',
                        icon: 'none'
                    })
                },
                complete: () => {
                    // uni.hideLoading();
					uni.stopPullDownRefresh();
                }
            });
        }catch(e){
            // uni.hideLoading();
            uni.showToast({
                title: '请检查网络连接',
                icon: 'none'
            })
        }
        
    })
}

export default http